<?php

class Talktangle_AjaxController extends Core_Controller_Action_Standard
{   
    public function previewtalktangleAction(){
        $content = $this->_getParam('content');
        if(!$content){
            return;
        }     
        $tangle = array();
        $words = explode(' ',$content);
        $i = 0;
        foreach($words as $word){
            $wordArray = str_split($word);
            shuffle($wordArray);
            $tangle[$i] = implode('',$wordArray);
            $i++;
        }
        $tangle = implode(' ',$tangle); 
        
        return $this->_helper->json($tangle);
        $this->getResponse()->setBody(Zend_Json::encode($tangle));               
    }
	
	public function uncrossAction(){
	    if( !$this->_helper->requireUser()->isValid() ) {
		  return $this->_helper->json ( array (
                'error' => !$ok ,
                'message' => 'required user.'
             )
          );
	    }
        $viewer = Engine_Api::_()->user()->getViewer();
        
	    $id = $this->_getParam('crossId',0);
        $key = $this->_getParam('key',array());
        $result = $this->_getParam('result',array());
        $answer = array_combine($key,$result);
        $coreApi =  Engine_Api::_()->talktangle();
        unset($answer['upm']);
        
        $ok = $coreApi->unCrosstangle($answer,$id);
     
        if($ok == true){
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();                
            try{ 
                $setting = Engine_Api::_()->getApi('settings','core');
                $point = $setting->getSetting('talktangle.uncrosstangle.point',2);
                $coreApi->addPoint($viewer,$point);
                $tangle = Engine_Api::_()->getItem('talktangle_crosstangle',$id);
                $coreApi->untangleNotification($tangle,$viewer);
                $coreApi->setStatus($tangle,'completed');
                $tangle->winner_id = $viewer->user_id;
                $tangle->save();
                
                $viewer->points = $viewer->points + $point;
                $viewer->untangle = $viewer->untangle + 1;
                $viewer->save();
                $db->commit();   
            }
            catch(Exception $e)
            {
                $db->rollback();
                return $this->_helper->json ( array (
                    'error' => !$ok ,
                    'message' => $e->getMessage()
                 )
                );
            }
        }
        return $this->_helper->json(array('error'=>!$ok));
        
	}
    
    public function unpicAction(){
        
        $picId = $this->_getParam('picId');
        $userId = $this->_getParam('userId');
        $steps = $this->_getParam('steps');
        
        $viewer = Engine_Api::_()->user()->getViewer();
        $coreApi = Engine_Api::_()->getApi('Core','talktangle');
        
        $picApi =  Engine_Api::_()->getApi('Ptangle','talktangle');
        $ok = $picApi->unPictangle($steps);
        if ($ok) {
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();                
            try{ 
                $setting = Engine_Api::_()->getApi('settings','core');
                $point = $setting->getSetting('talktangle.uncrosstangle.point',2);
                
                $tangle = Engine_Api::_()->getItem('talktangle_pictangle',$picId);
                $coreApi->untangleNotification($tangle,$viewer);
                $tangle->status = Talktangle_Api_Core::TANGLE_STATUS_COMPLETED;
                $tangle->winner_id = $viewer->user_id;
                $tangle->save();
                
                $viewer->points = $viewer->points + $point;
                $viewer->untangle = $viewer->untangle + 1;
                $viewer->save();
                $db->commit();   
            }
            catch(Exception $e)
            {
                $db->rollback();
                return $this->_helper->json ( array (
                    'error' => !$ok ,
                    'message' => $e->getMessage()
                 )
                );
            }  
        }
        return $this->_helper->json(array('error'=>!$ok));
    }
    
    public function unvidAction(){
        
        $picId = $this->_getParam('picId');
        $userId = $this->_getParam('userId');
        $steps = $this->_getParam('steps');
        
        $viewer = Engine_Api::_()->user()->getViewer();
        $coreApi = Engine_Api::_()->getApi('Core','talktangle');
        
        $picApi =  Engine_Api::_()->getApi('Ptangle','talktangle');
        $ok = $picApi->unPictangle($steps);
        if ($ok) {
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();                
            try{ 
                $setting = Engine_Api::_()->getApi('settings','core');
                $point = $setting->getSetting('talktangle.uncrosstangle.point',2);
                
                $tangle = Engine_Api::_()->getItem('talktangle_vidtangle',$picId);
                $coreApi->untangleNotification($tangle,$viewer);
                $tangle->status = Talktangle_Api_Core::TANGLE_STATUS_COMPLETED;
                $tangle->winner_id = $viewer->user_id;
                $tangle->save();
                
                $viewer->points = $viewer->points + $point;
                $viewer->untangle = $viewer->untangle + 1;
                $viewer->save();
                $db->commit();   
            }
            catch(Exception $e)
            {
                $db->rollback();
                return $this->_helper->json ( array (
                    'error' => !$ok ,
                    'message' => $e->getMessage()
                 )
                );
            }  
        }
        return $this->_helper->json(array('error'=>!$ok));
    }
    
}  
